home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-3.98 / gdb / target.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-01  |  13.6 KB  |  533 lines

  1. /* Select target systems and architectures at runtime for GDB.
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <ctype.h>
  24. #include "defs.h"
  25. #include "target.h"
  26. #include "gdbcmd.h"
  27. #include "symtab.h"
  28. #include "inferior.h"
  29. #include "bfd.h"
  30. #include "symfile.h"
  31.  
  32. extern int memory_insert_breakpoint(), memory_remove_breakpoint();
  33. extern void host_convert_to_virtual(), host_convert_from_virtual();
  34. extern void add_syms_addr_command();
  35.  
  36. static void cleanup_target ();
  37.  
  38. /* Pointer to array of target architecture structures; the size of the
  39.    array; the current index into the array; the allocated size of the 
  40.    array.  */
  41. struct target_ops **target_structs;
  42. unsigned target_struct_size;
  43. unsigned target_struct_index;
  44. unsigned target_struct_allocsize;
  45. #define    DEFAULT_ALLOCSIZE    10
  46.  
  47. /* The initial current target, so that there is always a semi-valid
  48.    current target.  */
  49.  
  50. struct target_ops dummy_target = {"None", "None", "",
  51.     0, 0, 0, 0,        /* open, close, attach, detach */
  52.     0, 0,        /* resume, wait */
  53.     0, 0, 0, 0, 0,    /* registers */
  54.     0, 0,         /* memory */
  55.     0, 0,         /* bkpts */
  56.     0, 0, 0, 0, 0,     /* terminal */
  57.     0, 0,         /* kill, load */
  58.     add_syms_addr_command,    /* add_syms */
  59.     0, 0,         /* call_function, lookup_symbol */
  60.     0, 0,        /* create_inferior, mourn_inferior */
  61.     dummy_stratum, 0,    /* stratum, next */
  62.     0, 0, 0, 0, 0,    /* all mem, mem, stack, regs, exec */
  63.     OPS_MAGIC,
  64. };
  65.  
  66. /* The target structure we are currently using to talk to a process
  67.    or file or whatever "inferior" we have.  */
  68.  
  69. struct target_ops *current_target;
  70.  
  71. /* The stack of target structures that have been pushed.  */
  72.  
  73. struct target_ops **current_target_stack;
  74.  
  75. /* Command list for target.  */
  76.  
  77. static struct cmd_list_element *targetlist = NULL;
  78.  
  79. /* The user just typed 'target' without the name of a target.  */
  80.  
  81. /* ARGSUSED */
  82. static void
  83. target_command (arg, from_tty)
  84.      char *arg;
  85.      int from_tty;
  86. {
  87.   fputs_filtered ("Argument required (target name).\n", stdout);
  88. }
  89.  
  90. /* Add a possible target architecture to the list.  */
  91.  
  92. void
  93. add_target (t)
  94.      struct target_ops *t;
  95. {
  96.   if (t->to_magic != OPS_MAGIC)
  97.     {
  98.       fprintf(stderr, "Magic number of %s target struct wrong\n", 
  99.     t->to_shortname);
  100.       abort();
  101.     }
  102.  
  103.   if (!target_structs)
  104.     {
  105.       target_struct_allocsize = DEFAULT_ALLOCSIZE;
  106.       target_structs = (struct target_ops **) xmalloc
  107.     (target_struct_allocsize * sizeof (*target_structs));
  108.     }
  109.   if (target_struct_size >= target_struct_allocsize)
  110.     {
  111.       target_struct_allocsize *= 2;
  112.       target_structs = (struct target_ops **) xrealloc (target_structs, 
  113.     target_struct_allocsize * sizeof (*target_structs));
  114.     }
  115.   target_structs[target_struct_size++] = t;
  116.   cleanup_target (t);
  117.  
  118.   if (targetlist == NULL)
  119.     add_prefix_cmd ("target", class_run, target_command,
  120.             "Connect to a target machine or process.\n\
  121. The first argument is the type or protocol of the target machine.\n\
  122. Remaining arguments are interpreted by the target protocol.  For more\n\
  123. information on the arguments for a particular protocol, type\n\
  124. `help target ' followed by the protocol name.",
  125.             &targetlist, "target ", 0, &cmdlist);
  126.   add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
  127. }
  128.  
  129. /* Stub functions */
  130.  
  131. static void
  132. ignore ()
  133. {
  134. }
  135.  
  136. /* ARGSUSED */
  137. static int
  138. nomemory (memaddr, myaddr, len, write)
  139.      CORE_ADDR memaddr;
  140.      char *myaddr;
  141.      int len;
  142.      int write;
  143. {
  144.   return 0;        /* No bytes handled */
  145. }
  146.  
  147. static void
  148. tcomplain ()
  149. {
  150.   error ("You can't do that when your target is `%s'",
  151.      current_target->to_shortname);
  152. }
  153.  
  154. static int
  155. noprocess ()
  156. {
  157.   error ("You can't do that without a process to debug");
  158. }
  159.  
  160. /* ARGSUSED */
  161. static int
  162. nosymbol (name, addrp)
  163.      char *name;
  164.      CORE_ADDR *addrp;
  165. {
  166.   return 1;        /* Symbol does not exist in target env */
  167. }
  168.  
  169. /* ARGSUSED */
  170. static void
  171. default_terminal_info (args, from_tty)
  172.      char *args;
  173.      int from_tty;
  174. {
  175.   printf("No saved terminal information.\n");
  176. }
  177.  
  178. #if 0
  179. /* With strata, this function is no longer needed.  FIXME.  */
  180. /* This is the default target_create_inferior function.  It looks up
  181.    the stack for some target that cares to create inferiors, then
  182.    calls it -- or complains if not found.  */
  183.  
  184. static void
  185. upstack_create_inferior (exec, args, env)
  186.      char *exec;
  187.      char *args;
  188.      char **env;
  189. {
  190.   struct target_ops *t;
  191.  
  192.   for (t = current_target;
  193.        t;
  194.        t = t->to_next)
  195.     {
  196.       if (t->to_create_inferior != upstack_create_inferior)
  197.     {
  198.           t->to_create_inferior (exec, args, env);
  199.       return;
  200.     }
  201.  
  202.     }
  203.   tcomplain();
  204. }
  205. #endif
  206.  
  207. /* This is the default target_create_inferior and target_attach function.
  208.    If the current target is executing, it asks whether to kill it off.
  209.    If this function returns without calling error(), it has killed off
  210.    the target, and the operation should be attempted.  */
  211.  
  212. static void
  213. kill_or_be_killed (from_tty)
  214.      int from_tty;
  215. {
  216.   /* FIXME: What is savecur for?  Why isn't it used?  */
  217.   struct target_ops *savecur;
  218.  
  219.   if (target_has_execution)
  220.     {
  221.       printf ("You are already running a program:\n");
  222.       target_files_info ();
  223.       if (query ("Kill it? ")) {
  224.     savecur = current_target;
  225.     target_kill (0, from_tty);
  226.     if (target_has_execution)
  227.       error ("Killing the program did not help.");
  228.     return;
  229.       } else {
  230.     error ("Program not killed.");
  231.       }
  232.     }
  233.   tcomplain();
  234. }
  235.  
  236. static void
  237. maybe_kill_then_attach (args, from_tty)
  238.      char *args;
  239.      int from_tty;
  240. {
  241.   kill_or_be_killed (from_tty);
  242.   target_attach (args, from_tty);
  243. }
  244.  
  245. static void
  246. maybe_kill_then_create_inferior (exec, args, env)
  247.      char *exec;
  248.      char *args;
  249.      char **env;
  250. {
  251.   kill_or_be_killed (0);
  252.   target_create_inferior (exec, args, env);
  253. }
  254.  
  255. /* Clean up a target struct so it no longer has any zero pointers in it.
  256.    We default entries, at least to stubs that print error messages.  */
  257.  
  258. static void
  259. cleanup_target (t)
  260.      struct target_ops *t;
  261. {
  262.  
  263.   /* Check magic number.  If wrong, it probably means someone changed
  264.      the struct definition, but not all the places that initialize one.  */
  265.   if (t->to_magic != OPS_MAGIC)
  266.     {
  267.       fprintf(stderr, "Magic number of %s target struct wrong\n", 
  268.     t->to_shortname);
  269.       abort();
  270.     }
  271.  
  272. #define de_fault(field, value) \
  273.   if (!t->field)    t->field = value
  274.  
  275.   /*        FIELD            DEFAULT VALUE        */
  276.  
  277.   de_fault (to_open,             tcomplain);
  278.   de_fault (to_close,             (void (*)())ignore);
  279.   de_fault (to_attach,             maybe_kill_then_attach);
  280.   de_fault (to_detach,             (void (*)())ignore);
  281.   de_fault (to_resume,             (void (*)())noprocess);
  282.   de_fault (to_wait,             noprocess);
  283.   de_fault (to_fetch_registers,     ignore);
  284.   de_fault (to_store_registers,        noprocess);
  285.   de_fault (to_prepare_to_store,    (void (*)())noprocess);
  286.   de_fault (to_convert_to_virtual,    host_convert_to_virtual);
  287.   de_fault (to_convert_from_virtual,    host_convert_from_virtual);
  288.   de_fault (to_xfer_memory,        nomemory);
  289.   de_fault (to_files_info,        ignore);
  290.   de_fault (to_insert_breakpoint,    memory_insert_breakpoint);
  291.   de_fault (to_remove_breakpoint,    memory_remove_breakpoint);
  292.   de_fault (to_terminal_init,        ignore);
  293.   de_fault (to_terminal_inferior,    ignore);
  294.   de_fault (to_terminal_ours_for_output,ignore);
  295.   de_fault (to_terminal_ours,        ignore);
  296.   de_fault (to_terminal_info,        default_terminal_info);
  297.   de_fault (to_kill,            (void (*)())noprocess);
  298.   de_fault (to_load,            tcomplain);
  299.   de_fault (to_add_syms,        tcomplain);
  300.   de_fault (to_call_function,        (struct value *(*)())noprocess);
  301.   de_fault (to_lookup_symbol,        nosymbol);
  302.   de_fault (to_create_inferior,        maybe_kill_then_create_inferior);
  303.   de_fault (to_mourn_inferior,        (void (*)())noprocess);
  304.   de_fault (to_next,            0);
  305.   de_fault (to_has_all_memory,        0);
  306.   de_fault (to_has_memory,        0);
  307.   de_fault (to_has_stack,        0);
  308.   de_fault (to_has_registers,        0);
  309.   de_fault (to_has_execution,        0);
  310.  
  311. #undef de_fault
  312. }
  313.  
  314. /* Push a new target type into the stack of the existing target accessors,
  315.    possibly superseding some of the existing accessors.
  316.  
  317.    Result is zero if the pushed target ended up on top of the stack,
  318.    nonzero if at least one target is on top of it.
  319.  
  320.    Rather than allow an empty stack, we always have the dummy target at
  321.    the bottom stratum, so we can call the function vectors without
  322.    checking them.  */
  323.  
  324. int
  325. push_target (t)
  326.      struct target_ops *t;
  327. {
  328.   struct target_ops *st, *prev;
  329.  
  330.   for (prev = 0, st = current_target;
  331.        st;
  332.        prev = st, st = st->to_next) {
  333.     if ((int)(t->to_stratum) >= (int)(st->to_stratum))
  334.       break;
  335.   }
  336.  
  337.   while (t->to_stratum == st->to_stratum) {
  338.     /* There's already something on this stratum.  Close it off.  */
  339.     (st->to_close) (0);
  340.     if (prev)
  341.       prev->to_next = st->to_next;    /* Unchain old target_ops */
  342.     else
  343.       current_target = st->to_next;    /* Unchain first on list */
  344.     st = st->to_next;
  345.   }
  346.  
  347.   /* We have removed all targets in our stratum, now add ourself.  */
  348.   t->to_next = st;
  349.   if (prev)
  350.     prev->to_next = t;
  351.   else
  352.     current_target = t;
  353.  
  354.   cleanup_target (current_target);
  355.   return prev != 0;
  356. }
  357.  
  358. /* Remove a target_ops vector from the stack, wherever it may be. 
  359.    Return how many times it was removed (0 or 1 unless bug).  */
  360.  
  361. int
  362. unpush_target (t)
  363.      struct target_ops *t;
  364. {
  365.   struct target_ops *u, *v;
  366.   int result = 0;
  367.  
  368.   for (u = current_target, v = 0;
  369.        u;
  370.        v = u, u = u->to_next)
  371.     if (u == t)
  372.       {
  373.     if (v == 0)
  374.       pop_target();            /* unchain top copy */
  375.     else {
  376.       (t->to_close)(0);        /* Let it clean up */
  377.       v->to_next = t->to_next;    /* unchain middle copy */
  378.     }
  379.     result++;
  380.       }
  381.   return result;
  382. }
  383.  
  384. void
  385. pop_target ()
  386. {
  387.   (current_target->to_close)(0);    /* Let it clean up */
  388.   current_target = current_target->to_next;
  389.   if (!current_target)        /* At bottom, push dummy.  */
  390.     push_target (&dummy_target);
  391. }
  392.  
  393. /* Move memory to or from the targets.  Iterate until all of it has
  394.    been moved, if necessary.  The top target gets priority; anything
  395.    it doesn't want, is offered to the next one down, etc.  Note the
  396.    business with curlen:  if an early target says "no, but I have a
  397.    boundary overlapping this xfer" then we shorten what we offer to
  398.    the subsequent targets so the early guy will get a chance at the
  399.    tail before the subsequent ones do. 
  400.  
  401.    Result is 0 or errno value.  */
  402.  
  403. int
  404. target_read_memory (memaddr, myaddr, len)
  405.      CORE_ADDR memaddr;
  406.      char *myaddr;
  407.      int len;
  408. {
  409.   return target_xfer_memory (memaddr, myaddr, len, 0);
  410. }
  411.  
  412. int
  413. target_write_memory (memaddr, myaddr, len)
  414.      CORE_ADDR memaddr;
  415.      char *myaddr;
  416.      int len;
  417. {
  418.   return target_xfer_memory (memaddr, myaddr, len, 1);
  419. }
  420.  
  421. int
  422. target_xfer_memory (memaddr, myaddr, len, write)
  423.      CORE_ADDR memaddr;
  424.      char *myaddr;
  425.      int len;
  426.      int write;
  427. {
  428.   int curlen;
  429.   int res;
  430.   struct target_ops *t;
  431.   
  432.   /* The quick case is that the top target does it all.  */
  433.   res = current_target->to_xfer_memory(memaddr, myaddr, len, write);
  434.   if (res == len)
  435.     return 0;
  436.  
  437.   if (res > 0)
  438.     goto bump;
  439.   /* If res <= 0 then we call it again in the loop.  Ah well.  */
  440.  
  441.   for (; len > 0;)
  442.     {
  443.       curlen = len;        /* Want to do it all */
  444.       for (t = current_target;
  445.        t;
  446.        t = t->to_has_all_memory? 0: t->to_next)
  447.     {
  448.       res = t->to_xfer_memory(memaddr, myaddr, curlen, write);
  449.       if (res > 0) break;    /* Handled all or part of xfer */
  450.       if (res == 0) continue;    /* Handled none */
  451.       curlen = -res;    /* Could handle once we get past res bytes */
  452.     }
  453.       if (res <= 0)
  454.     {
  455.       /* If this address is for nonexistent memory,
  456.          read zeros if reading, or do nothing if writing.  Return error. */
  457.       if (!write)
  458.         bzero (myaddr, len);
  459.       return EIO;
  460.     }
  461. bump:
  462.       memaddr += res;
  463.       myaddr  += res;
  464.       len     -= res;
  465.     }
  466.   return 0;            /* We managed to cover it all somehow. */
  467. }
  468.  
  469.  
  470. /* ARGSUSED */
  471. static void
  472. target_info (args, from_tty)
  473.      char *args;
  474.      int from_tty;
  475. {
  476.   struct target_ops *t;
  477.   int has_all_mem = 0;
  478.   
  479.   if (symfile != 0)
  480.     printf ("Symbols from \"%s\".\n", symfile);
  481.  
  482. #ifdef FILES_INFO_HOOK
  483.   if (FILES_INFO_HOOK ())
  484.     return;
  485. #endif
  486.  
  487.   for (t = current_target;
  488.        t;
  489.        t = t->to_next)
  490.     {
  491.       if ((int)(t->to_stratum) <= (int)dummy_stratum)
  492.     continue;
  493.       if (has_all_mem)
  494.     printf("\tWhile running this, gdb does not access memory from...\n");
  495.       printf("%s:\n", t->to_longname);
  496.       (t->to_files_info)();
  497.       has_all_mem = t->to_has_all_memory;
  498.     }
  499. }
  500.  
  501. /* This is to be called by the open routine before it does
  502.    anything.  */
  503.  
  504. void
  505. target_preopen (from_tty)
  506.      int from_tty;
  507. {
  508.   dont_repeat();
  509.  
  510.   if (target_has_execution)
  511.     {   
  512.       if (query ("A program is being debugged already.  Kill it? "))
  513.         target_kill ((char *)0, from_tty);
  514.       else
  515.         error ("Program not killed.");
  516.     }
  517. }
  518.  
  519. static char targ_desc[] = 
  520.     "Names of targets and files being debugged.\n\
  521. Shows the entire stack of targets currently in use (including the exec-file,\n\
  522. core-file, and process, if any), as well as the symbol file name.";
  523.  
  524. void
  525. _initialize_targets ()
  526. {
  527.   current_target = &dummy_target;
  528.   cleanup_target (current_target);
  529.  
  530.   add_info ("target", target_info, targ_desc);
  531.   add_info ("files", target_info, targ_desc);
  532. }
  533.